package xyz.comfyz.web.commons.security.support;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import xyz.comfyz.web.commons.security.model.Authentication;
import xyz.comfyz.web.commons.security.provider.TokenProvider;
import xyz.comfyz.web.commons.utils.WebContextUtil;

/**
 * @author :    comfy
 * @date :      2018-05-12 16:04:49
 * @since :     1.8
 * <p>
 * 工具
 */
@Component
public abstract class SecurityUtils {

    public static String signIn(Authentication token) {
        if (token == null)
            token = SecurityUser.ANONYMOUS.authentication();
        String secret = SecretUtils.encrypt(token.getSecurityToken());
        TokenProvider.flush(secret);
        return secret;
    }

    public static void signOut() {
        TokenProvider.remove(TokenProvider.get());
    }

    public static String getRequestPath() {
        String url = WebContextUtil.request().getServletPath();
        if (WebContextUtil.request().getPathInfo() != null) {
            url = url + WebContextUtil.request().getPathInfo();
        }
        return url;
    }

    public static HttpMethod valueOf(String method) {
        try {
            return HttpMethod.valueOf(method);
        } catch (IllegalArgumentException var2) {
            return null;
        }
    }

}
